package com.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/11/6
 * @Description:
 */
public class DB {
    private String driverClass="com.mysql.cj.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/briup";
    private String username="briup";
    private String password="briup";
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    //构造方法:初始化数据库连接
    public DB() {
        try {
            Class.forName(driverClass);
            conn=java.sql.DriverManager.getConnection(url,username,password);
            this.stmt=this.conn.createStatement();
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取数据库操作对象
    public Statement getStations() {
        return this.stmt;
    }
    //关闭数据库连接
    public void close(){
        try {
            if(rs!=null) rs.close();
            if(stmt!=null) stmt.close();
            if(conn!=null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void setRs(ResultSet rs){
        this.rs=rs;
    }
    public void close(ResultSet rs){
        this.setRs(rs);
        this.close();
    }

}
